unit ESetDatabaseIni;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TfmSetDatabase = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    edDB: TEdit;
    edHost: TEdit;
    btSave: TBitBtn;
    btCancel: TBitBtn;
    cbDbType: TComboBox;
    Label4: TLabel;
    Label5: TLabel;
    edUser: TEdit;
    edPwd: TEdit;
    Label6: TLabel;
    edPort: TEdit;
    procedure btCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure edDBEnter(Sender: TObject);
    procedure edDBExit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmSetDatabase: TfmSetDatabase;

implementation
uses Inifiles,uEncrypt, Utility;

{$R *.dfm}

procedure TfmSetDatabase.btCancelClick(Sender: TObject);
begin
  close;
end;

procedure TfmSetDatabase.FormCreate(Sender: TObject);
var fReg:TIniFile;
  sDecrypted:AnsiString;
begin
  fReg:=TIniFile.create(ExtractFilePath(Application.ExeName)+'IDocqment.ini' );
  try
    edDb.text:=fReg.ReadString( 'DB', 'Database', 'idocqment');
    edHost.text:=fReg.ReadString( 'DB', 'HostName', '127.0.0.1');
    cbDbType.ItemIndex:=cbDbType.Items.IndexOf(fReg.ReadString( 'DB', 'Protocol', 'mysql'));
    edUser.text:=fReg.ReadString( 'DB', 'Users', 'root');
    sDecrypted := Decrypt(fReg.ReadString( 'DB', 'Password', ''),my_key);
    edPwd.text:=sDecrypted;
    edPort.text:=fReg.ReadString( 'DB', 'Port', '3306');
  finally
    fReg.free;
    fReg:=nil;
  end;
end;

procedure TfmSetDatabase.btSaveClick(Sender: TObject);
var fReg:TIniFile;
  sEncrypted:AnsiString;
begin
  fReg:=TIniFile.create(ExtractFilePath(Application.ExeName)+'IDocqment.ini' );
  try
    fReg.WriteString( 'DB', 'Database', trim(edDb.text));
    fReg.WriteString( 'DB', 'HostName', trim(edHost.text));
    fReg.WriteString( 'DB', 'Protocol', Trim(cbDbType.text));
    fReg.WriteString( 'DB', 'Users', trim(edUser.text));

    sEncrypted := Encrypt(trim(edPwd.text),my_key); 
    fReg.WriteString( 'DB', 'Password', sEncrypted);
    fReg.WriteString( 'DB', 'Port', trim(edPort.text));
  finally
    fReg.free;
    fReg:=nil;
  end;
  close;
end;

procedure TfmSetDatabase.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=VK_Up then SelectNext(ActiveControl,false,true);
  if Key=VK_Down then SelectNext(ActiveControl,true,true);
end;

procedure TfmSetDatabase.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then SelectNext(ActiveControl,true,true);
end;

procedure TfmSetDatabase.edDBEnter(Sender: TObject);
begin
  SetBackColor(sender,true);
end;

procedure TfmSetDatabase.edDBExit(Sender: TObject);
begin
  SetBackColor(sender,false);
end;

end.
